package org.yxjme.exjni_two.a;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.Gson;

import org.yxjme.exjni_two.R;
import org.yxjme.exjni_two.u.SdcardUtil;
import org.yxjme.exjni_two.u.SoundPoolUtil;
import org.yxjme.exjni_two.v.CameraSurfaceView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

public class TakePhotoActivity extends AppCompatActivity  implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_photo);
        findViewById(R.id.btn_takePhoto).setOnClickListener(this);
        image = findViewById(R.id.image);

        initCamera();
    }


    private ImageView image;
    private Camera camera;
    private CameraSurfaceView mCameraSurfaceView;
    private String TAG="[initParameters]->";

    public void initCamera(){
        mCameraSurfaceView = findViewById(R.id.mCameraSurfaceView);
        /**
         * 获取相机实例
         */
        camera = Camera.open();
        initParameters();
        mCameraSurfaceView.init(camera);
    }


    /**
     * 参数
     *
     * 这里可以配置一系列的参数
     */
    private void initParameters() {
        Camera.Parameters params = camera.getParameters();
        /**获取所有支持预览的分辨率*/
        List<Camera.Size> supportedPreviewSizes = params.getSupportedPreviewSizes();
        Log.v(TAG,"supportedPreviewSizes = "+new Gson().toJson(supportedPreviewSizes));
        /**获取当前设备支持的相片尺寸*/
        List<Camera.Size> supportedPictureSizes= params.getSupportedPictureSizes();
        Log.v(TAG,"supportedPictureSizes = "+new Gson().toJson(supportedPictureSizes));
        /**设置预览的分辨率*/
//        params.setPreviewSize(176,144);
        /**设置图片大小*/
//        params.setPictureSize();
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        /**图片*/
        params.setPictureFormat(ImageFormat.JPEG);
        /**图片质量*/
//        params.setJpegQuality(100);
        camera.setParameters(params);
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (camera!=null){
            camera.stopPreview();
            camera.release();
            camera = null ;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_takePhoto:
                if (camera!=null){
                    takePhoto();
                }
                break;
        }
    }


    public void takePhoto(){
        Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {

            public void onShutter() {
                Log.d(TAG, "onShutter: ");
            }
        };

        Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {

            public void onPictureTaken(final byte[] data, final Camera c) {
                camera.startPreview();
                Log.d(TAG, "onPictureTaken: ");

                File f = new File(SdcardUtil.getRootFilePath(TakePhotoActivity.this)
                        +File.separator+ UUID.randomUUID()+".png");
                try {
                    FileOutputStream outputStream = new FileOutputStream(f);
                    outputStream.write(data,0,data.length);
                    outputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            camera.enableShutterSound(false);
        }
        camera.takePicture(shutterCallback, null, jpegCallback);
        MediaPlayer mediaPlayer=MediaPlayer.create(TakePhotoActivity.this,R.raw.beep);
        mediaPlayer.start();
    }
}